import { IAppUITheme, ISysPFPlugin } from '@ibiz/model-core';
import { clone } from 'ramda';
import { CustomThemeUtil, ICustomThemeState } from './custom-theme-util';

/**
 * 主题工具类
 *
 * @author chitanda
 * @date 2023-12-02 23:12:15
 * @export
 * @class ThemeUtil
 */
export class ThemeUtil {
  /**
   * 主题设置元素 html
   *
   * @author chitanda
   * @date 2023-12-02 23:12:26
   * @protected
   * @type {HTMLElement}
   */
  // protected html: HTMLElement = document.getElementsByTagName('html')[0];

  /**
   * 自定义主题工具类
   *
   * @protected
   * @type {CustomThemeUtil}
   * @memberof ThemeUtil
   */
  protected customUtil: CustomThemeUtil = {} as CustomThemeUtil;

  /**
   * 加载主题插件
   *
   * @author chitanda
   * @date 2023-12-03 01:12:44
   * @param {IAppUITheme} theme
   * @return {*}  {Promise<void>}
   */
  async loadTheme(theme: IAppUITheme): Promise<void> {
    const data = clone(theme.themeParams || {});
    const path = data['theme-package-path'];
    delete data.appId;
    delete data['theme-package-path'];
    await ibiz.plugin.loadPlugin({
      runtimeObject: true,
      rtobjectName: theme.name,
      rtobjectRepo: path,
    } as unknown as ISysPFPlugin);
    this.setThemeParams(theme, data);
    this.setTheme(theme.themeTag!);
  }

  /**
   * 设置额外修改的主题参数
   *
   * @author chitanda
   * @date 2023-12-05 11:12:00
   * @protected
   * @param {IAppUITheme} theme
   * @param {Record<string, string>} params
   * @return {*}  {void}
   */
  protected setThemeParams(
    theme: IAppUITheme,
    params: Record<string, string>,
  ): void {
    // const themeStyle = document.getElementById(theme.themeTag!);
    // if (themeStyle) {
    //   return;
    // }
    // let content = `:root.${theme.themeTag!}{`;
    // for (const key in params) {
    //   if (Object.prototype.hasOwnProperty.call(params, key)) {
    //     const val = params[key];
    //     content += `${key}: ${val}${val.endsWith(';') ? '' : ';'}`;
    //   }
    // }
    // content += '}';
    // const script = document.createElement('style');
    // script.id = theme.themeTag!;
    // script.type = 'text/css';
    // script.innerHTML = content;
    // document.head.appendChild(script);
  }

  /**
   * 设置主题
   *
   * @author chitanda
   * @date 2023-12-02 23:12:37
   * @param {string} tag
   */
  setTheme(tag: string): void {
    // const theme = this.getTheme();
    // this.html.classList.remove(theme);
    // this.html.setAttribute('theme', tag);
    // this.html.classList.add(tag);
  }

  /**
   * 获取当前主题
   *
   * @author chitanda
   * @date 2023-12-02 23:12:10
   * @return {*}  {string}
   */
  getTheme(): string {
    // return this.html.getAttribute('theme')!;
    return ''
  }

  /**
   * 自定义主题
   *
   * @memberof ThemeUtil
   */
  customTheme(): void {
    ibiz.overlay.drawer('IBizCustomTheme', undefined, {
      width: 30,
      placement: 'right',
    });
  }

  /**
   * 初始化自定义主题
   *
   * @return {*}  {Promise<void>}
   * @memberof ThemeUtil
   */
  async initCustomTheme(needLoad: boolean = true): Promise<void> {
    this.customUtil = new CustomThemeUtil(this);
    if (needLoad) {
      await this.customUtil.loadCustomTheme();
    }
  }

  /**
   * 获取自定义主题
   *
   * @return {*}  {ICustomThemeState}
   * @memberof ThemeUtil
   */
  getCustomTheme(): ICustomThemeState {
    return this.customUtil.state;
  }

  /**
   * 设置自定义主题
   *
   * @param {IData} data
   * @memberof ThemeUtil
   */
  setCustomTheme(data: IData): void {
    Object.assign(this.customUtil.state, data);
  }

  /**
   * 预览自定义主题
   *
   * @param {string} themeTag
   * @param {Record<string, string>} themeVars
   * @return {*}  {ICustomThemeState}
   * @memberof ThemeUtil
   */
  previewCustomTheme(
    themeTag: string,
    themeVars: Record<string, string>,
  ): ICustomThemeState {
    return this.customUtil.previewCustomTheme(themeTag, themeVars);
  }

  /**
   * 还原自定义主题
   *
   * @return {*}  {void}
   * @memberof ThemeUtil
   */
  resetCustomTheme(): void {
    return this.customUtil.clearCustomThemeParams();
  }

  /**
   * 保存自定义主题
   *
   * @param {string} themeTag
   * @param {Record<string, string>} themeVars
   * @return {*}  {(Promise<boolean | undefined>)}
   * @memberof ThemeUtil
   */
  async saveCustomTheme(
    themeTag: string,
    themeVars: Record<string, string>,
  ): Promise<boolean | undefined> {
    return this.customUtil.saveCustomTheme(themeTag, themeVars);
  }

  /**
   * 分享自定义主题
   *
   * @param {string} themeTag
   * @param {Record<string, string>} themeVars
   * @return {*}  {(Promise<string | undefined>)}
   * @memberof ThemeUtil
   */
  async shareCustomTheme(
    themeTag: string,
    themeVars: Record<string, string>,
  ): Promise<string | undefined> {
    return this.customUtil.shareCustomTheme(themeTag, themeVars);
  }

  /**
   * 获取分享主题
   *
   * @param {string} userId
   * @param {string} themeId
   * @return {*}  {Promise<IData>}
   * @memberof ThemeUtil
   */
  async getShareTheme(userId: string, themeId: string): Promise<IData> {
    return this.customUtil.getShareTheme(userId, themeId);
  }
}
